<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\api\controller\shop;

use app\api\controller\shop\Admin;

/**
 * @title 【收货地址】
 */
class Address extends Admin {

    /**
     * @title 收货地址列表
     * @method post
     * 
     */
    public function lists() {

        //{"list":[{"address_id":203,"uid":500,"name":"小明","telephone":"13116524343","address":"1111111111111111111111","city_id":36,"country_id":37,"province_id":1}]}
        // $str = '{"list":[{"address_id":203,"uid":500,"name":"小明","telephone":"13116524343","address":"1111111111111111111111","city_id":36,"country_id":37,"province_id":1}]}';
        // echo $str;
        // $arr = json_decode($str, true);


        $map = array('search_init' => array('member_id' => UID));

        $address_list = db('member_address')->where('member_id', UID)->order('id desc')->select();



        //print_r($address_list);
        foreach ($address_list as $key => $value) {


            $address_list[$key]['address'] = $value['province'] . $value['city'] . $value['county'] . $value['detail'];

            $address_list[$key]['default'] = ($value['default'] == 1) ? true : false;
        }

        return ['code' => 0, 'msg' => 'success', 'data' => $address_list];

        //return json(['list' => $address_list]);
    }

    /**
     * @title 设置某个地址为默认
     * @method post
     * @params address_id 0
     */
    public function set_default() {

        $address_id = input('post.address_id', 0);

        if ($address_id) {

            db('member_address')->where('member_id', UID)->setField('default', 0);
            db('member_address')->where('member_id', UID)->where('id', $address_id)->setField('default', 1);

            return ['code' => 0, 'msg' => 'success', 'data' => 1];
        } else {
            return ['code' => 1, 'msg' => 'params missing'];
        }
    }

    /**
     * @title 查
     * @method post
     * @params address_id 0
     */
    public function read() {

        $address_id = input('post.address_id', 0);

        if ($address_id) {

            $one = db('member_address')->where('id', $address_id)->find();

            return ['code' => 0, 'msg' => 'success', 'data' => $one];
        } else {
            return ['code' => 1, 'msg' => 'params missing'];
        }
    }

    /**
     * @title 地址 增/改
     * @method post
     * @params address_detail 杭州市西湖区 黄龙万科中心
     * @params area_code 130302
     * @params city 秦皇岛市
     * @params county 海港区
     * @params default true
     * @params name 收货人
     * @params postal_code 100000
     * @params province 河北省
     * @params tel 18994094765
     * @params address_id 0 如果为0，则添加否则更新
     */
    public function create() {


        //最多10个收货地址
        $count = db('member_address')->where('id', UID)->count();
        if ($count > 20) {
            return ['code' => 10, 'msg' => '收货地址最多保存20个'];
        }


        $post = input('post.');

        $data['member_id'] = UID;

        $data['name'] = $post['name'];
        $data['phone'] = $post['tel'];
        $data['detail'] = $post['addressDetail'];

        $data['province'] = $post['province'];
        $data['city'] = $post['city'];
        $data['county'] = $post['county'];

        $data['area_code'] = $post['areaCode'] ?? ''; // 非必填
        $data['postal_code'] = $post['postalCode'] ?? ''; // 非必填
        //
        // 分情况处理     
        if (isset($post['address_id']) && !empty($post['address_id'])) {

            // 更新
            $data['update_time'] = time();

            $affect_rows = db('member_address')->where('id', $post['address_id'])->update($data);

            return ['code' => 0, 'msg' => 'success', 'data' => $affect_rows];
        } else {

            // 新增
            $data['default'] = $post['isDefault'] ?? 0; // 非必填
            $data['create_time'] = time();
            $data['update_time'] = time();

            $insert_id = db('member_address')->insertGetId($data);

            return ['code' => 0, 'msg' => 'success', 'data' => $insert_id];
        }


        // print_r($post);
    }

    /**
     * @title 删
     * @method post
     * @params address_id 0
     */
    public function delete() {

        $address_id = input('post.address_id', 0);

        if ($address_id) {
            $affect_rows = db('member_address')->where('id', $address_id)->delete();
            return ['code' => 0, 'msg' => 'success', 'data' => $affect_rows];
        } else {
            return ['code' => 1, 'msg' => 'params missing'];
        }
    }

}
